package a02_链表;

import java.util.List;

/**
 * <p>
 * a08_环形链表复习6
 * </p>
 *
 * @author flyduck
 * @since 2025/4/14
 */
public class a08_环形链表复习6 {
    public ListNode detectCycle(ListNode head) {
        ListNode slowNode = head;
        ListNode fastNode = head;

        while (fastNode != null && fastNode.next != null){
           slowNode = slowNode.next;
           fastNode = fastNode.next.next;

           if(slowNode == fastNode){
               ListNode node1 = head;
               ListNode node2 = slowNode;
               while (node1 != node2){
                   node1 = node1.next;
                   node2 = node2.next;
               }
               return node1;
           }
        }

        return null;
    }
}
